#include "LASER_RANGING.H"

static u8 Res_2;
static u8 USART2_RX_sta;
static u8 USART2_Package[LASER_PACKAGE_LEN];

static u8 Res_3;
static u8 USART3_RX_sta;
static u8 USART3_Package[LASER_PACKAGE_LEN];

/*********************************
 * 激光初始化
 * @date 2023 3.13
 * @name 吴昕灿
 * @param void 
 * @return void
**********************************/
void LASER_RANGING_Init(void)
{
    USART_2_Init(115200);
    USART_3_Init(115200);
}

/*********************************
 * 激光uart2发送(y)
 * @date 2023 3.13
 * @name 吴昕灿
 * @param void 
 * @return void
**********************************/
void USART2_Send(u8*data,u8 size)
{
    u8 Temp;
    for(Temp = 0;Temp<size;Temp++)
    {
        while(USART_GetFlagStatus(USART2,USART_FLAG_TC) != SET);
        USART_SendData(USART2,data[Temp]);
    }
}

/*********************************
 * 激光uart3发送(x)
 * @date 2023 3.13
 * @name 吴昕灿
 * @param void 
 * @return void
**********************************/
void USART3_Send(u8*data,u8 size)
{
    static u8 Temp;
    for(Temp = 0;Temp<size;Temp++)
    {
        USART_SendData(USART3,data[Temp]);
        while(USART_GetFlagStatus(USART3,USART_FLAG_TC) != SET);
        
    }
}
/*********************************
 * 激光数据读取命令发送
 * @date 2023 3.5
 * @name 吴昕灿
 * @param void 
 * @return void
**********************************/
void LASER_RANGING_Send_instructions(void)
{   //缓冲

    u8 data[8] = {0x50,0x03,0x00,0x34,0x00,0x01,0xc8,0x45};
    //50 03 00 34 00 01 c8 45
    USART2_Send(data,8);
    USART3_Send(data,8);
}





/*********************************
 * 激光数据读取
 * @date 2023 3.13
 * @name 吴昕灿
 * @param void 
 * @return distance:Y轴距离
**********************************/
u16 distance_y_Read(void)
{
    u16 distance;
        distance = USART2_Package[4];
        distance |= USART2_Package[3]<<8;
        return distance;
}

/*********************************
 * 激光数据读取
 * @date 2023 3.13
 * @name 吴昕灿
 * @param void 
 * @return distance:X轴距离
**********************************/
unsigned int distance_x_Read(void)
{
    static u16 distance;

        distance = USART3_Package[4];
        distance |= USART3_Package[3]<<8;

        return distance;

}



/*
激光数据y轴读取中断
*/
void USART2_IRQHandler(void)
{  

    static u8 Temp;
    if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
    {   //接收中断
        Res_2 =USART_ReceiveData(USART2);//(USART2->DR);    //读取接收到的数据
        if(USART2_RX_sta == 0)  //接收未开始
        {
            if(Res_2 == LASER_PACKAGE_HEAD)
            {
                USART2_RX_sta = 1;

            }
            else 
            {
                USART2_RX_sta = 0;
            } 
        }
        if(USART2_RX_sta == 1)//开始接收
        { 
            
            if(Temp == (LASER_PACKAGE_LEN-1))
            {
                //USART2_Package[Temp] = LASER_PACKAGE_END;
                USART2_RX_sta = 0;//接收完成 
                Temp = 0;

            }
            else
            {
                USART2_Package[Temp] = Res_2;
                Temp++;

            } 
        }
    }
}

/*
激光数据x轴读取中断
*/
void USART3_IRQHandler(void)
{  

    static u8 Temp;
    if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
    {   //接收中断
        Res_3 =USART_ReceiveData(USART3);//(UART3->DR);    //读取接收到的数据
        if(USART3_RX_sta == 0)  //接收未开始
        {
            if(Res_3 == LASER_PACKAGE_HEAD)
            {
                USART3_RX_sta = 3;

            }
            else 
            {
                USART3_RX_sta = 0;
            } 
        }
        if(USART3_RX_sta == 3)//开始接收
        { 
            
            if(Temp == (LASER_PACKAGE_LEN-1))
            {
                //USART2_Package[Temp] = LASER_PACKAGE_END;
                USART3_RX_sta = 0;//接收完成 
                Temp = 0;

            }
            else
            {
                USART3_Package[Temp] = Res_3;
                Temp++;

            } 
        }
   }
}


